<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" :close-on-click-modal="false" @close="closeDialog" @open="openDialog">
    <el-checkbox-group v-model="roleIds" v-loading="isLoading">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="assignRolesAPIFn">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getBaseUserInfoAPI } from '@/api/user'
import { assignRolesAPI } from '@/api/employees'
export default {
  name: 'AasignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleIds: [],
      pag: 1,
      size: 999,
      roleList: [],
      isLoading: false
    }
  },
  methods: {
    async openDialog() {
      this.isLoading = true
      await this.getRoleListApiFn()
      await this.getBaseUserInfoAPIFn()
      this.isLoading = false
    },
    /* async openDialog() {
      this.isLoading = true
      Promise.all([this.getRoleListApiFn(), this.getBaseUserInfoAPIFn()]).then(() => {
        this.isLoading = false
      })
    }, */
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    },
    async getRoleListApiFn() {
      const { data } = await getRoleListApi(this.pag, this.size)
      this.roleList = data.rows
    },
    async getBaseUserInfoAPIFn() {
      const { data: { roleIds }} = await getBaseUserInfoAPI(this.userId)
      this.roleIds = roleIds
    },
    async assignRolesAPIFn() {
      await assignRolesAPI({ id: this.userId, roleIds: this.roleIds })
      this.closeDialog()
      this.$message.success('分配成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>

